<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/20
 * Time: 14:28
 */

namespace app\api\controller;


use think\Controller;
use think\Db;

class Pay extends Controller
{

    public function Pay($openid, $out_trade_no, $body, $total_fee, $spbill_create_ip)
    {
        $notify_url = request()->domain() . url('api/Pay/notify_url');
        $ret = Db::name('pay')->find();
        $appid = $ret['appid'];        //准备数据（都在后台系统中可查到）
        $mch_id = $ret['mch_id'];      //商户号
        $key = $ret['key'];
        $weixinpay = new WeiXinPay($appid, $openid, $mch_id, $key, $out_trade_no, $body, $total_fee, $notify_url, $spbill_create_ip);
        $return = $weixinpay->pay();
        return $return;
    }

    public function notify_url()
    {
        //获取接口数据，如果$_REQUEST拿不到数据，则使用file_get_contents函数获取
        $post = input('request.');
        if ($post == null) {
            $post = file_get_contents("php://input");
        }
        libxml_disable_entity_loader(true); //禁止引用外部xml实体
        $xml = simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);//XML转数组
        $post_data = (array)$xml;
        //订单号
        $out_trade_no = isset($post_data['out_trade_no']) && !empty($post_data['out_trade_no']) ? $post_data['out_trade_no'] : 0;
        //查询订单信息
        $order_info = Db::name('order_sku')->where('order_id',$out_trade_no)->find();

        if (count($order_info) > 0) {
            //查询平台信息
            $platform_info = Db::name('pay')->find();
            //平台支付key
            $wxpay_key = $platform_info['key'];
            //接收到的签名
            $post_sign = $post_data['sign'];
            unset($post_data['sign']);
            //重新生成签名
            $newSign = $this->MakeSign($post_data, $wxpay_key);
            //签名统一，则更新数据库
            $str = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
            if ($post_sign == $newSign) {
                if ($post_data['result_code'] == 'SUCCESS'){
                    $branch = new Branch();
                    $order = Db::name('order_sku')->where(['order_id'=>$out_trade_no])->find();
                    $user  = Db::name('member_user')->where(['id'=>$order['user_id']])->find();
                    $ret = Db::name('order_sku')->where('order_id',$out_trade_no)->update(['status'=>1]);
                    $collage = Db::name('collage_people')->where('order_id',$out_trade_no)->find();
                    if ($collage){
                        $arr = Db::name('collage_group')->where('collage_id',$collage['collage_id'])->find();
                        if ($arr['people'] == $arr['speople']){
                            Db::name('collage_group')->where('collage_id',$collage['collage_id'])->update(['status'=>2]);
                        }
                    }
                    $asd = $branch->brokerageEdit($user['user_id'],$user['pid'],$order['order_id'],1);
                    if ($ret && $asd){
                        echo $str;
                    }
                }
            }
        }

    }





    public function MakeSign($params,$key){
        //签名步骤一：按字典序排序数组参数
        ksort($params);
        $string = $this->ToUrlParams($params);  //参数进行拼接key=value&k=v
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=".$key;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }

    public function ToUrlParams($params)
    {
        $string = '';
        if (!empty($params)) {
            $array = array();
            foreach ($params as $key => $value) {
                $array[] = $key . '=' . $value;
            }
            $string = implode("&", $array);
        }
        return $string;
    }




}